/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;

import org.easypr.organizer.client.presenter.SearchPresenter;
import org.easypr.organizer.shared.MyContact;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class SearchView.
 */
public class SearchView extends Composite implements SearchPresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The search input. */
	TextBox searchInput;
	
	/** The search button. */
	Button searchButton;
	
	/** The dropdown search. */
	FlowPanel dropdownSearch;
	
	/** The dropdown search table. */
	FlexTable dropdownSearchTable;
	
	/** The dropdown selected. */
	int dropdownSelected;
	
	/** The dropdown selected row. */
	int dropdownSelectedRow;
	
	/** The dropdown last direction. */
	int dropdownLastDirection;

	/**
	 * Instantiates a new menu view.
	 */
	public SearchView() {
		container = new FlowPanel();
		initWidget(container);
		searchInput = new TextBox();
		searchButton = new Button("Search");
		dropdownSearch = new FlowPanel();
		dropdownSearchTable = new FlexTable();
		dropdownSelected = 0;
		dropdownSelectedRow = 0;
		dropdownLastDirection = 0;
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getDropdownLastDirection()
	 */
	public int getDropdownLastDirection() {
		return this.dropdownLastDirection;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#setDropdownLastDirection(int)
	 */
	public void setDropdownLastDirection(int direction) {
		this.dropdownLastDirection = direction;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#setDropdownSelectedRow(int)
	 */
	public void setDropdownSelectedRow(int currentRow) {
		this.dropdownSelectedRow = currentRow;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getDropdownSelectedRow()
	 */
	public int getDropdownSelectedRow() {
		return this.dropdownSelectedRow;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#setDropdownSelected(int)
	 */
	public void setDropdownSelected(int selected) {
		this.dropdownSelected = selected;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getDropdownSelected()
	 */
	public int getDropdownSelected() {
		return this.dropdownSelected;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getDropdownSearchTable()
	 */
	public FlexTable getDropdownSearchTable() {
		return this.dropdownSearchTable;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getSearchInput()
	 */
	public TextBox getSearchInput() {
		return this.searchInput;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#getSearchButton()
	 */
	public Button getSearchButton() {
		return this.searchButton;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#setLiveSearchData(java.util.ArrayList)
	 */
	public void setLiveSearchData(ArrayList<MyContact> contacts) {
		dropdownSearch.clear();
		String html;
		if(contacts.size() == 0) {
			dropdownSearch.setVisible(false);
		} else {
			
		}
		//dropdownSearchTable.clear();
		dropdownSearchTable = new FlexTable();
		dropdownSearchTable.setWidth("100%");
		
		for(int i=0; i<contacts.size(); i++) {
			
			if(contacts.get(i).getWorkplace().length() > 0) {
				html = contacts.get(i).getName() + " - <i>" + contacts.get(i).getWorkplace()+"</i>";
			} else {
				html = contacts.get(i).getName();
			}
			HTML contact = new HTML(html);
			contact.getElement().setId(Long.toString(contacts.get(i).getId()));
			dropdownSearchTable.setWidget(i, 0, contact);
		}
		dropdownSearch.setVisible(true);
		dropdownSearch.add(dropdownSearchTable);
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#setData(java.lang.String)
	 */
	public void setData(String searchString) {
		container.clear();
		dropdownSearch.addStyleName("dropdown-search");
		
		
		HorizontalPanel subContainer = new HorizontalPanel();
		searchInput.setWidth("200px");
		if(searchString != null) {
			searchInput.setText(searchString);
		}
		subContainer.add(searchInput);
		subContainer.add(searchButton);
		
		dropdownSearch.setVisible(false);
		container.add(dropdownSearch);
		container.add(subContainer);
		container.add(new Label("Search for people and notes"));
		
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.SearchPresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}
}
